var grid = "";

$(function() {
	var exportBtn = $("#exportBtn").val(); //导出权限按钮控制
	var viewCheck = $("#viewCheck").val(); //表格权限按钮控制
	
	var dtGridColumns = [ {
		id : 'id',
		title : '编号',
		type : 'string',
		columnClass : 'text-center',
		headerStyle : 'color:grey;'
	}, {
		id : 'serialNo',
		title : '订单号',
		type : 'string',
		columnClass : 'text-center',
		headerStyle : 'color:grey;'
	}, {
		id : 'mobile',
		title : '用户名',
		type : 'string',
		columnClass : 'text-center',
		headerStyle : 'color:grey;'
	}, {
		id : 'realName',
		title : '借款人姓名',
		type : 'string',
		columnClass : 'text-center',
		headerStyle : 'color:grey;'
	}, {
		id : 'expectRepayTime',
		title : '预期还款日期',
		type : 'string',
		columnClass : 'text-center',
		headerStyle : 'color:grey;'
	}, {
		id : 'orderCreateTime',
		title : '发起还款日期',
		type : 'string',
		columnClass : 'text-center',
		headerStyle : 'color:grey;'
	}, {
		id : 'benxi',
		title : '应还本息',
		type : 'string',
		columnClass : 'text-center',
		headerStyle : 'color:grey;'
	}, {
		id : 'faxi',
		title : '合计逾期罚息',
		type : 'string',
		columnClass : 'text-center',
		headerStyle : 'color:grey;'
	}, {
		id : 'orderAmount',
		title : '合计应还总额',
		type : 'string',
		columnClass : 'text-center',
		headerStyle : 'color:grey;'
	},{
		id : 'typeDesc',
		title : '还款类型',
		type : 'string',
		columnClass : 'text-center',
		headerStyle : 'color:grey;'
	},{
		id : 'resMsg',
		title : '失败原因',
		type : 'string',
		columnClass : 'text-center',
		headerStyle : 'color:grey;'
	},{
		id : 'operation',
		title : '详情',
		type : 'string',
		columnClass : 'text-center tpl-table-black-operation',
		headerStyle : 'color:grey;',
		resolution : function(value, record, column, grid, dataNo, columnNo) {
	        var content = '';
	        content += '<a href="#" onclick="showRepayment(\''+record.type+'\',\''+record.id+'\');"> <i class="am-icon-search"></i>查看</a> ';
	        return content;
		}
	}];
	
	var dtGridOption = {
		lang : 'zh-cn',
		ajaxLoad : true,
		check : true,
		checkWidth : '37px',
	    myExport : true,
	    tools : exportBtn?'export[excel]':'',
	    exportURL:'repayment/failureListExport',
		loadURL : 'repayment/failureList',
		columns : dtGridColumns,
		gridContainer : 'dtGridContainer',
		toolbarContainer : 'dtGridToolBarContainer',
		pageSize : 20,
		pageSizeLimit : [ 10, 20, 30 ]
	};
	grid = $.fn.dlshouwen.grid.init(dtGridOption);
	grid.load();
	$('.form-datetime-lang').datepicker({
	    language:  'zh-CN',
	    format: 'yyyy-mm-dd',
	    autoclose: true
	});
});

function search() {
	  grid.pager.startRecord = 0;
	  grid.pager.nowPage = 1;
	grid.parameters = new Object();
	grid.parameters['mobile'] = $("#mobile").val();
	grid.parameters['realName'] = $("#realName").val();
	grid.parameters['repayOrderType'] = $("#repayOrderType").val();
	grid.parameters['startTime'] = $("#startTime").val();
	grid.parameters['endTime'] = $("#endTime").val();
	grid.refresh(true);
}


function btnReoperate(){
	batchReoperate(search);
}

function batchReoperate(callback){
	var rows = grid.getCheckedRecords();
	if (rows.length == 0) {
		layer.msg("你没有选择行", {
	         icon : 0
	     });
		return;
	 }
	var ids ='';
	for(var i=0;i<rows.length;i++){
		ids +=","+rows[i].id;
	}
	layer.confirm('是否确认请求扣款？', {
        icon : 3,
        title : '请求扣款'
    }, function(index, layero) {
        $.ajax({
            type : "POST",
            url :  'repayment/reWithhold',
            dataType : "json",
            data: {"ids":ids.substr(1)},
            success : function(resultdata) {
                if (resultdata.ret=="200") {
                    layer.msg(resultdata.message, {
                        icon : 1
                    });
                    if (callback) {
                        callback();
                    }
                } else {
                    layer.msg(resultdata.message, {
                        icon : 0
                    });
                }
            },
            error : function(errorMsg) {
                layer.msg('服务器未响应,请稍后再试', {
                    icon : 3
                });
            }
        });
        layer.close(index);
    });
}

function showRepayment(status,orderId){
	$.ajax({
        type : "get",
        url :  "repayment/order2repayment/"+orderId,
        dataType : "json",
        success : function(resultdata) {
            if (resultdata.ret=="200") {
            	layer.open({
            	  title:'还款详情',
            	  type: 1,
            	  skin: 'layui-layer-rim', //加上边框
            	  area: ['980px', '740px'], //宽高
            	  content: getTable(status,resultdata.content.info)
            	});
            } else {
                layer.msg(resultdata.message, {
                    icon : 0
                });
            }
        },
        error : function(errorMsg) {
            layer.msg('服务器未响应,请稍后再试', {
                icon : 3
            });
        }
    });

}

//组装回款详情
function getTable(status,data){
	var ary = [];
	ary.push('<table class="am-table am-table-bordered" style="width:960px;margin: 3px">');
	ary.push('<thead><tr>');
	ary.push('<th>序号</th>');
	ary.push('<th>借款金额</th>');
	ary.push('<th>借款期限</th>');
	ary.push('<th>还款日</th>');
	if('advance' == status){
		ary.push('<th>剩余待还本金</th>');
		ary.push('<th>计息天数</th>');
	}else{
		ary.push('<th>起息时间</th>');
		ary.push('<th>还款期数</th>');
		ary.push('<th>预期还款日期</th>');
	}
	ary.push('<th>应还本息</th>');
	ary.push('<th>到账日期</th>');
	ary.push('<th>逾期天数</th>');
	ary.push('<th>逾期罚息</th>');
	ary.push('<th>应收总额</th>');
	ary.push('</tr></thead>');
	ary.push('<tbody>');
	
	for(i=0;i<data.length;i++){
		ary.push('<tr>');
		ary.push('<td>'+(i+1)+'</td>');
		ary.push('<td>'+data[i].bowAmount+'</td>');
		ary.push('<td>'+data[i].repayMonth+'</td>');
		ary.push('<td>每月'+data[i].repay_day+'日</td>');
		if('advance' == status){
			ary.push('<td>'+data[i].principal+'</td>');
			ary.push('<td>'+data[i].advanceInteDay+'</td>');
		}else{
			ary.push('<td>'+data[i].releaseTime+'</td>');
			ary.push('<td>'+data[i].curPerriodNum+'</td>');
			ary.push('<td>'+data[i].expectRepayTime+'</td>');
		}
		ary.push('<td>'+data[i].benxi+'</td>');
		ary.push('<td>'+data[i].settleDay+'</td>');
		ary.push('<td>'+data[i].overdueDay+'</td>');
		ary.push('<td>'+data[i].faxi+'</td>');
		ary.push('<td>'+data[i].repayAmount+'</td>');
		ary.push('</tr>');
	}
	ary.push('</tbody></table>');
	return ary.join("");
}



